﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections;
using System.Windows.Controls;
using System.Windows.Media;
using CommomFunctions;

namespace Árvore_Binária_de_Pesquisa
{
    public enum ButtonClicked
    {
        Complete,
        Create,
        Destroy,
        Insert,
        Remove,
        Search
    }

    public class Buttons : CommomFunctions.Buttons
    {
        /// <summary>
        /// Updates the disponibility of the buttons accolording to "all" and "createAndComplete"
        /// </summary>
        /// <param name="btns"></param>
        /// <param name="all">
        /// Indicates if all button should be enabled or disabled
        /// </param>
        /// <param name="createAndComplete">
        /// Indicates if the buttons "btnCreate" and "btnComplete" should be enabled or disabled
        /// </param>
        public static void updateButtons(Button[] btns, bool all, bool createAndComplete)
        {
            if (all)
                habilitaBotoes(btns);
            else
                desabilitaBotoes(btns);

            btns[(int)ButtonClicked.Complete].IsEnabled = createAndComplete;
            btns[(int)ButtonClicked.Create].IsEnabled = createAndComplete;
        }

        /// <summary>
        /// Calls the function that will show a complete tree and updates the buttons disponibility
        /// </summary>
        /// <param name="btns"></param>
        /// <param name="cv"></param>
        public static void btnExhibitComplete_Click(Button[] btns, ControlVariables cv)
        {
            cv.showCompleteTree();
            updateButtons(btns, true, false);
        }

        /// <summary>
        /// Calls the function that will create a new empty tree and updates the buttons disponibility
        /// </summary>
        /// <param name="btns"></param>
        /// <param name="cv"></param>
        public static void btnCreate_Click(Button[] btns, ControlVariables cv)
        {
            cv.createEmptyTree();
            updateButtons(btns, true, false);
        }

        /// <summary>
        /// Calls the function that will destroy the existent tree and updates the buttons disponibility
        /// </summary>
        /// <param name="btns"></param>
        /// <param name="cv"></param>
        public static void btnDestroy_Click(Button[] btns, ControlVariables cv)
        {
            cv.destroyTree();
            updateButtons(btns, false, true);
        }

        /// <summary>
        /// Calls the function that tests if the tree or the node information are empty.
        /// If it returns true it means the requested operation can not be performed.
        /// </summary>
        /// <param name="testNumber"></param>
        /// <param name="cv"></param>
        /// <returns></returns>
        private static bool callTests(int testNumber, ControlVariables cv)
        {
            switch (testNumber)
            {
                case 1: if (cv.treeIsEmpty())
                            return true;
                   break;
                    
                case 2: if (cv.getInputText() == "")
                        {
                            MessageBox.Show("Informe o nodo desejado.");
                            return true;
                        }
                    break;
            }
            return false;
        }

        /// <summary>
        /// Calls the function that will try to insert a node in the existent tree
        /// </summary>
        /// <param name="cv"></param>
        public static void btnInsere_Click(ControlVariables cv)
        {
            if (callTests(2, cv))
               return;

            cv.insertTreeNode();
        }

        /// <summary>
        /// Calls the function that will search for a node in the existent tree
        /// </summary>
        /// <param name="cv"></param>
        public static void btnProcura_Click(ControlVariables cv)
        {
            if ((callTests(1,  cv)) || (callTests(2,  cv)))
                return;

            cv.startSearchTreeNodeAnimation();
        }

        /// <summary>
        /// Calls the function that will try to remove a node in the existent tree 
        /// </summary>
        /// <param name="cv"></param>
        public static void btnRemove_Click(ControlVariables cv)
        {
            if ((callTests(1, cv)) || (callTests(2, cv)))
                return;
            
            cv.startRemoveTreeNodeAnimation();
        }
    }
}
